package com.zaicheng.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author 张在成
 */
@Configuration
public class KeyResolverConfiguration {

    /**
     * 限流规则,基于uri
     */
/*    @Bean
    public KeyResolver pathKeyResolver(){
        return new KeyResolver() {
            @Override
            public Mono<String> resolve(ServerWebExchange exchange) {
                return Mono.just(exchange.getRequest().getPath().toString());
            }
        };
    }*/

    /**
     * 限流规则,基于参数
     */
/*    @Bean
    public KeyResolver paramterKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("id"));
    }*/

    /**
     * 限流规则,基于ip
     */
    @Bean
    public KeyResolver ipKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
    }
}
